function Promise(executor){
    this.status = 'pending'
    this.value = null
    this.reason = null
    this.onResolvedArr = [] 
    this.onRejectedArr = []
    
    const resolve = value => {
        setTimeout(() => {
            if(this.status === 'pending'){
                this.value = value
                this.status = 'fulfilled'
                this.onRejectedArr.forEach(func => {
                    func(value)
                })
            }
        })
    }
    const reject = reason => {
        setTimeout(() => {
            if(this.status === 'pending'){
                this.value = reason
                this.status = 'rejected'
                this.onRejectedArr.forEach(func => {
                    func(reason)
                })
            }
        })
    }
    try {
        executor(resolve, reject)
    } catch(e) {
        reject(e)
    }
}

Promise.prototype.then = function (onfulfilled, onrejected){
    onfulfilled = typeof onfulfilled === 'function' ? onfulfilled : data => data
    onrejected = typeof onrejected === 'function' ? onrejected : error => {throw error}
    if(this.status === 'fulfilled'){
        onfulfilled(this.value)
    }
    if(this.status === 'rejected'){
        onrejected(this.reason)
    }
    if(this.status === 'pending'){
        this.onResolvedArr.push(onfulfilled)
        this.onRejectedArr.push(onrejected)
    }
}